package com.xyzq.simpson.apm.web.utils;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @Description:
 * @date 2021/4/27 9:07
 */
public class DurationUtil {

    public static long getStartTimeStamp(Map<String, Object> json) throws Exception {
        Map<String, Object> duration = (Map<String, Object>)json.get("duration");
        String start = String.valueOf(duration.get("start"));
        String step = String.valueOf(duration.get("step"));
        if("DAY".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd").parse(start).getTime();
        } else if("MINUTE".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HHmm").parse(start).getTime();
        } else if("HOUR".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HH").parse(start).getTime();
        }
        return 0;
    }

    public static long getEndTimeStamp(Map<String, Object> json) throws Exception {
        Map<String, Object> duration = (Map<String, Object>)json.get("duration");
        String end = String.valueOf(duration.get("end"));
        String step = String.valueOf(duration.get("step"));
        if("DAY".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd").parse(end).getTime();
        } else if("MINUTE".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HHmm").parse(end).getTime();
        } else if("HOUR".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HH").parse(end).getTime();
        }
        return 0;
    }

    public static long getStartTimeStampFromTraceDuration(Map<String, Object> json) throws Exception {
        Map<String, Object> duration = (Map<String, Object>)json.get("queryDuration");
        String start = String.valueOf(duration.get("start"));
        String step = String.valueOf(duration.get("step"));
        if("DAY".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd").parse(start).getTime();
        } else if("MINUTE".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HHmm").parse(start).getTime();
        } else if("HOUR".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HH").parse(start).getTime();
        }
        return 0;
    }

    public static long getEndTimeStampFromTraceDuration(Map<String, Object> json) throws Exception {
        Map<String, Object> duration = (Map<String, Object>)json.get("queryDuration");
        String end = String.valueOf(duration.get("end"));
        String step = String.valueOf(duration.get("step"));
        if("DAY".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd").parse(end).getTime();
        } else if("MINUTE".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HHmm").parse(end).getTime();
        } else if("HOUR".equals(step)) {
            return new SimpleDateFormat("yyyy-MM-dd HH").parse(end).getTime();
        }
        return 0;
    }
}
